import {Component, OnInit} from '@angular/core';
import {Product, ProductService} from '../shared/product.service';
import {LogService} from '../shared/log.service';
import {AnotherProductService} from '../shared/another-product.service';
import {Address, AddressService} from '../shared/address.service';
@Component({
    selector: 'app-product3',
    templateUrl: './product3.component.html',
    styleUrls: ['./product3.component.css'],
    //这个提供器同时也可以在app.module.ts里声明,一样的
    providers: [
        {
            provide: ProductService,
            useFactory: () => {
                let log = new LogService();
                //当随机数大于5的时候,出现苹果手机的信息
                if (Math.random() > .5) {
                    return new ProductService(log);
                    //当随机数小于5的时候,出现三星手机的信息
                } else {
                    return new AnotherProductService();
                }
            }
        },
        {
            provide: AddressService,
            useFactory: (log: LogService, price) => {
                log.log(price);
                return new AddressService(log);
            },
            deps: [LogService, 'SOME_VAR']
        },
        {
            provide: 'SOME_VAR', useValue: '价格是50元'
        }
    ]
})
export class Product3Component implements OnInit {
    public product: Product;
    public address: Address;

    constructor(public productService: ProductService,
                public addressService: AddressService) {
    }

    ngOnInit() {
        this.product = this.productService.getProduct();
        this.address = this.addressService.getAddress()
    }
}
